package com.cskaoyan.typeHanlder;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @program: stone-exception-test
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-27 15:15
 **/


public class UserTypeHandler extends BaseTypeHandler<Integer[]> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Integer[] parameter, JdbcType jdbcType) throws SQLException {

    }

    @Override
    public Integer[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // 得到 [1,2.3,4]
        String string = rs.getString(columnName);
        // 转换为 integer数组
        String[] split = string.substring(1, string.length() - 1).split(",");
        Integer[] ints = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            ints[i] = Integer.parseInt(split[i]);
        }
        return ints;
    }

    @Override
    public Integer[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return new Integer[0];
    }

    @Override
    public Integer[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return new Integer[0];
    }
}
